
var Cluster = Class.create( {
		cCapCount: 0,
		nCapWidth: 0,
		
		nCurCap: 0,
		bInScroll: false,
		bSuppressScrolling: false,
		rgImages: Array(),
		rgImageURLs: {},
		nCapsulesToPreload: 1,
		
		initialize: function( args )
		{
			this.cCapCount = args.cCapCount;
			this.nCapWidth = args.nCapWidth;
			this.nCapsulesToPreload = args.nCapsulesToPreload || 1;
			
			//DOM对象
			this.elClusterArea = args.elClusterArea;
			this.elScrollArea = args.elScrollArea || this.elClusterArea.down('.cluster_scroll_area');
			//this.elScrollAreaMini = args.elScrollArea || this.elClusterArea.down('.cluster_scroll_area_mini');//mini
			this.elScrollLeftBtn = args.elScrollLeftBtn || this.elClusterArea.down('.cluster_control_left');
			this.elScrollRightBtn = args.elScrollRightBtn  || this.elClusterArea.down('.cluster_control_right');
			
			this.rgImages = args.rgImages || this.elClusterArea.select( 'img.cluster_capsule_image' );
			this.rgImageURLs = args.rgImageURLs || {};

			
			this.elScrollLeftBtn.observe( 'click', this.scrollLeft.bindAsEventListener( this, this.elScrollArea) );
			this.elScrollRightBtn.observe( 'click', this.scrollRight.bindAsEventListener( this, this.elScrollArea, false ) );
			//this.elScrollLeftBtn.observe( 'click', this.scrollLeft.bindAsEventListener( this, this.elScrollAreaMini) );//mini
			//this.elScrollRightBtn.observe( 'click', this.scrollRight.bindAsEventListener( this, this.elScrollAreaMini, false ) );//mini

			// put this in a closure
			var obj = this;

	     	Event.observe( window, 'load', this.startTimer.bind( this ) );
		},
		
		//自动滚动
		startTimer: function()
		{
			this.clearInterval();
			this.interval = window.setInterval( this.scrollRight.bindAsEventListener( this, elScrollArea, true ), 5000 );
		},

		clearInterval: function()
		{
			if ( this.interval )
			{
				window.clearInterval( this.interval );
				this.interval = false;
			}
		},
		
		scrollRight: function( event, elScrollArea, bAutoScroll )
		{
			if ( this.bSuppressScrolling && bAutoScroll )
				return;
			this.nCurCap++;
			this.bInScroll = true;
			var nDuration = bAutoScroll ? 0.4 : 0.4;
			if ( this.nCurCap <= this.cCapCount )
			{
				if ( elScrollArea.effect ) elScrollArea.effect.cancel();
				elScrollArea.effect = new Effect.Morph( elScrollArea, { style: 'left: -' + (this.nCurCap * this.nCapWidth) + 'px;', duration: nDuration, fps: 60 } );
			}
			else
			{							
				this.nCurCap = 0;
				if ( elScrollArea.effect ) elScrollArea.effect.cancel();
				var elScrollArea = elScrollArea;
				elScrollArea.effect = new Effect.Morph( elScrollArea, { style: 'left: -' + ( (this.cCapCount + 1 ) * this.nCapWidth) + 'px;', duration: nDuration, fps: 60, afterFinish: function() { elScrollArea.style.left = '0px'; } } );
			}
			this.slider.setValue( this.nCurCap * this.nCapWidth );
			this.ensureImagesLoaded();
			this.bInScroll = false;
		},
		
		scrollLeft: function( event, elScrollArea)
		{
			this.nCurCap--;
			this.bInScroll = true;
			if ( this.nCurCap >= 0 )
			{
				if ( elScrollArea.effect ) elScrollArea.effect.cancel();
				elScrollArea.effect = new Effect.Morph( elScrollArea, { style: 'left: -' + (this.nCurCap * this.nCapWidth) + 'px;', duration: 0.4 } );
			}
			else
			{							
				this.nCurCap = this.cCapCount;
				if ( elScrollArea.effect ) elScrollArea.effect.cancel();
				elScrollArea.style.left = '-' + ( ( this.cCapCount + 1 ) * this.nCapWidth ) + 'px';
				elScrollArea.effect = new Effect.Morph( elScrollArea, { style: 'left: -' + ( this.cCapCount * this.nCapWidth) + 'px;', duration: 0.4 } );
			}
			this.slider.setValue( this.nCurCap * this.nCapWidth );
			this.ensureImagesLoaded();
			this.bInScroll = false;
		},
	
		//预先加载
		ensureImagesLoaded: function()
		{
			for ( var i = 0; i <= this.nCapsulesToPreload && this.nCurCap + i < this.rgImages.length; i++ )
			{
				var img = this.rgImages[ this.nCurCap + i ];
				if ( this.rgImageURLs[ img.id ] && img.src != this.rgImageURLs[ img.id ] )
					img.src = this.rgImageURLs[ img.id ];
			}
		}
	} );



